package br.ufal.questione.dao.hibernate;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import br.ufal.questione.dao.AvaliacaoDao;
import br.ufal.questione.dao.DaoException;
import br.ufal.questione.domain.Avaliacao;
import br.ufal.questione.domain.Avaliador;
import br.ufal.questione.domain.Questionario;

@Repository
public class HibernateAvaliacaoDao extends HibernateGenericDao<Avaliacao, Long> implements AvaliacaoDao {

	@Override
	public Avaliacao consultarPorAvaliador(Avaliador avaliador, Questionario questionario) {
		try {
			return (Avaliacao) getSession().createCriteria(getClassEntidade())
			.add(Restrictions.eq("avaliador", avaliador))
			.add(Restrictions.eq("questionario", questionario))
			.uniqueResult();
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	@Override
	public boolean hasAnswered(Avaliador avaliador, Questionario questionario) {
		try {
			Long count = (Long) getSession().createCriteria(getClassEntidade())
			.add(Restrictions.eq("avaliador", avaliador))
			.add(Restrictions.eq("questionario", questionario))
			.setProjection(Projections.rowCount())
			.uniqueResult();
			
			return count > 0;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

}
